using System;
using System.Collections.Generic;
using System.Text;

namespace SharpWars.Core.Network
{
	public class StatsData
	{
		public long SendCount;
		public long ReceivedCount;
		public long SendBytes;
		public long ReceivedBytes;
	}

	public class Statistics
	{
		public Dictionary<string, StatsData> Packets = new Dictionary<string, StatsData>();
		public StatsData Total = new StatsData();
		
		public event EventHandler Update = null;
		
		public Statistics()
		{
			foreach( string name in Enum.GetNames( typeof( Command ) ) )
			{
				this.Packets.Add( name, new StatsData() );
			}
		}
		
		public void Receive( Packet packet )
		{
			Packets[packet.Command.ToString()].ReceivedCount++;
			Packets[packet.Command.ToString()].ReceivedBytes += packet.Size;
			Total.ReceivedCount++;
			Total.ReceivedBytes += packet.Size;
			
			if( Update != null )
			{
				Update( this, EventArgs.Empty );
			}
		}
		
		public void Send( Packet packet )
		{
			Packets[packet.Command.ToString()].SendCount++;
			Packets[packet.Command.ToString()].SendBytes += packet.Size;
			Total.SendCount++;
			Total.SendBytes += packet.Size;
			
			if( Update != null )
			{
				Update( this, EventArgs.Empty );
			}
		} 
	}
}
